package com.htt.bms.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author 侯婷婷
 * @version 1.0
 * @description
 */
@Slf4j
@Configuration
@EnableSwagger2 //开启swagger2 自动配置
public class SwaggerConfig {

    //如果需要配置多个分组，就配置多个docket()方法，如docket1(),docket2()...
    @Bean
    public Docket docket(Environment environment) {
        //通过内置tocket,配置swagger基本信息
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());

        //配置接口扫描信息,希望不要展示 basic-error-controller 相关的接口
        docket.select()//设置扫描接口
                .apis(RequestHandlerSelectors
                        .basePackage("com.htt.bms.controller")//扫描指定包下的接口，默认扫描全部接口
                        //.any()
                )
                .paths(PathSelectors
                        .any()//满足条件的路径，该断言为true
                )
                .build();

        //配置分组信息
        docket.groupName("htt");//修改组名为htt

        //控制swagger开启环境为dev，test
        //设置环境范围
        Profiles profiles = Profiles.of("dev", "test");
        //如果在该环境内返回true,否则返回false
        boolean flag = environment.acceptsProfiles(profiles);
        //是否开启swager
        docket.enable(flag);

        log.info("swagger docket={},配置完成",docket.getClass());
        return docket;
    }

    //如果需要配置多个分组，就配置多个docket()方法，如docket1(),docket2()...
    @Bean
    public Docket docket_test(Environment environment) {
        //通过内置tocket,配置swagger基本信息
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());

        //配置接口扫描信息,希望不要展示 basic-error-controller 相关的接口
        docket.select()//设置扫描接口
                .apis(RequestHandlerSelectors
                                .basePackage("com.htt.bms.testcontroller")//扫描指定包下的接口，默认扫描全部接口
                        //.any()
                )
                .paths(PathSelectors
                        .any()//满足条件的路径，该断言为true
                )
                .build();

        //配置分组信息
        docket.groupName("htt-test");//修改组名为htt

        //控制swagger开启环境为dev，test
        //设置环境范围
        Profiles profiles = Profiles.of("dev", "test");
        //如果在该环境内返回true,否则返回false
        boolean flag = environment.acceptsProfiles(profiles);
        //是否开启swager
        docket.enable(flag);

        log.info("swagger docket={},配置完成",docket.getClass());
        return docket;
    }
    
    //基本信息配置
    private ApiInfo apiInfo(){
        Contact contact = new Contact(
                "houtingting",//作者姓名
                "https://blog.csdn.net/weixin_45683778/article/details/136211224?spm=1001.2014.3001.5502",//作者主页
                "2894033643@qq.com");//作者邮箱
        return new ApiInfoBuilder()
                .title("侯婷婷-接口文档")//标题
                .description("后台管理系统接口文档")//描述
                .termsOfServiceUrl("https://www.baidu.com")//跳转到百度
                .version("1.0")//版本
                .license("swagger-的使用（详细教程）")
                .licenseUrl("https://www.baidu.com")
                .contact(contact)
                .build();
    }
}
